package com.cwt_taxi_xa.util;

import android.media.AudioManager;

import com.cwt_taxi_xa.MainApplication;

/**
 * 音量控制
 * @author ferch
 *
 */
public class VolumeControl {
	/**
	 * 音量控制manager
	 */
	public static AudioManager audiomanage=(AudioManager)MainApplication.getInstance().getSystemService(MainApplication.getInstance().AUDIO_SERVICE) ;
	public static int volvar=-1;
	public static boolean fromSerialport = false;

	/**
	 * 获取当前音量
	 * @return
	 */
	public static int getSystemVol(){

		return audiomanage.getStreamVolume(AudioManager.STREAM_SYSTEM);
	}

	/**
	 * 获取当前音量
	 * @return
	 */
	public static int getSystemVol(int streamType){

		return audiomanage.getStreamVolume(streamType);
	}

	/**
	 * 设置音量
	 */
	public static void setVol(int streamMode, int lev){

		audiomanage.setStreamVolume(streamMode, lev, 0);
	}

	/**
	 * 设置系统静音
	 */
	public static void setRingerMode(){
		if(audiomanage.getRingerMode() == AudioManager.RINGER_MODE_SILENT){
			audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		}else{
			audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		}
	}

	public static boolean flag = false;
	/**
	 * 设置系统静音模式
	 * @param switcher	0:静音	1:恢复
	 */
	public static void setRingerMode(int switcher){
		switch (switcher) {
			case 0:
				// 如果不静音，设为静音
				if(audiomanage.getRingerMode() == AudioManager.RINGER_MODE_NORMAL){
					audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
					flag = true;
				}
				break;
			case 1:
				if(flag){
					audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				}
				break;
		}
	}
	/**
	 * 控制音量
	 * @param mode 模式
	 * @param percent 音量大小百分比
	 * @param type  控制类型0为减小，1为增加
	 */
	public static void changeVol(int mode,int percent,int type){

		if(audiomanage.getRingerMode() == AudioManager.RINGER_MODE_SILENT){
			audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		}
		if(mode==0){
			if(type==0){
				//				audiomanage.adjustVolume(AudioManager.ADJUST_LOWER , AudioManager.FLAG_SHOW_UI);
				audiomanage.adjustSuggestedStreamVolume(AudioManager.ADJUST_LOWER, AudioManager.STREAM_MUSIC, AudioManager.FLAG_SHOW_UI);
			}else if(type==1){
				//				audiomanage.adjustVolume(AudioManager.ADJUST_RAISE , AudioManager.FLAG_SHOW_UI);
				audiomanage.adjustSuggestedStreamVolume(AudioManager.ADJUST_RAISE, AudioManager.STREAM_MUSIC, AudioManager.FLAG_SHOW_UI);
			}
		}else if(mode==1){
			/*Intent intent = new Intent("com.dss.app.carcall.ACTION_VOL");
			intent.putExtra("progress", percent);
			CarCallApplication.getInstance().sendBroadcast(intent);*/
			//			if(volvar==-1){
			//				volvar=percent;
			if(percent>=100){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 15, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=96){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 14, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=91){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 13, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=84){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 12, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=77){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 11, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=70){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 10, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=63){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 9, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=56){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 8, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=49){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 7, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=42){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 6, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=35){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 5, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=28){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 4, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=21){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 3, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=14){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 2, AudioManager.FLAG_SHOW_UI);
			}else if(percent>=7){
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 1, AudioManager.FLAG_SHOW_UI);
			}else{
				audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);
			}
			//			}
			//			if(volvar!=-1){
			//				if(type==0){
			//					audiomanage.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			//					playSound();
			//				}else if(type==1){
			//					audiomanage.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			//					playSound();
			//				}
			//			}
		}
		/*//最大音量
		int maxMusic = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		//当前音量
		int currentMusic = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC);
		System.out.println("maxMusic="+maxMusic+", currentMusic="+currentMusic);

		//最大音量
		int maxRing = audiomanage.getStreamMaxVolume(AudioManager.STREAM_RING);
		//当前音量
		int currentRing = audiomanage.getStreamVolume(AudioManager.STREAM_RING);
		System.out.println("maxRing="+maxRing+", currentRing="+currentRing);

		//最大音量
		int maxSys = audiomanage.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
		//当前音量
		int currentSys = audiomanage.getStreamVolume(AudioManager.STREAM_SYSTEM);
		System.out.println("maxSys="+maxSys+", currentSys="+currentSys);

		System.out.println("mode="+mode+", percent="+percent+", volvar"+volvar);*/
	}
	/**
	 * 播放按键音效
	 */
	public static void playSound(){
		MainApplication.sp.play(MainApplication.music, 1, 1, 0, 0, 1);
	}
}
